//
//  BookMarkRow.swift
//  test
//
//  Created by  mahoPunch on 2021/6/8.
//

import SwiftUI

struct BookmarkRow: View {
    
    typealias onBookmarkFetch = (_ bookmarkID: UUID) -> Void
    typealias emptyClosure = () -> Void
    
    
    //@Binding var bookmarkContext: [Bookmark]
    //行界面数据来源
    var bookmark: Bookmark
    var isDirectory: Bool
    
    @Binding var canRowBeSelected: Bool
    @Binding var isMoveMode: Bool
    var isSelected: Bool
    
    var onEditClickClosure: emptyClosure?
    var onDeleteClickClosure: onBookmarkFetch?
    var onBookmarkFolderClickClosure: onBookmarkFetch?
    var onBookmarkUrlClickClosure: onBookmarkFetch?
    var onBookmarkMoveClosure: emptyClosure?

    
//    var bookmark: BookmarkModel
//    var id: Int
//    var siteIcon: Image
//    var siteName: String
//    var url: String
//    var type: String
//
    
    var body: some View {
        
        
        HStack {
            
            if let data = bookmark.img {
                var img = UIImage(data: data)
                Image(uiImage: img!)
                    .resizable()
                    .frame(width: 40, height: 40, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(Color(.systemGray))
            }
            else {
                Image(systemName: isDirectory ? "folder.circle" : "bookmark.circle")
                    .resizable()
                    .frame(width: 40, height: 40, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(Color(.systemGray))
            }
            
            
            
            VStack(alignment: .leading){
                
                Text(bookmark.name)
                    .lineLimit(1)
                    
                Text(isDirectory ? "" : bookmark.url!)
                    .font(.footnote)
                    .foregroundColor(Color(.systemGray2))
                    .lineLimit(1)
            }
            Spacer()
            
            //Text(bookmark.id)
            
            if canRowBeSelected{
                Image(systemName: isSelected ? "checkmark.circle" : "circle")
                    .resizable()
                    .frame(width: canRowBeSelected ? CGFloat(20) : CGFloat(0), height: 20, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    .padding(.trailing, 2)
                    .foregroundColor(.blue)
                    .aspectRatio(contentMode: .fit)
            }
            else {
                /*
                 Bug:
                 Menu 不仅会接受点击，同时也会把此事件继续传递
                 */
                Menu(content: {
                    Button(action: {
                        //编辑按钮,传入当前值弹出编辑窗口
                        onEditClickClosure?()

                    }){
                        Image(systemName: "square.and.pencil")
                            .foregroundColor(Color(.systemBlue))
                        Text("编辑")
                    }
                    .padding(.trailing, 5)
                    
                    //删除按钮
                    Button(action: {
                        onDeleteClickClosure?(bookmark.id)
                    }){
                        Image(systemName: "trash")
                        Text("删除")
                    }
                    .foregroundColor(.red)
                    
                    if !isMoveMode{
                        Button(action: {
                            //移动按钮,进入移动模式
                            onBookmarkMoveClosure?()

                        }){
                            Image(systemName: "tray.and.arrow.up")
                                .foregroundColor(Color(.systemBlue))
                            Text("移动到...")
                        }
                        .padding(.trailing, 5)
                    }
                    
                }){
                    Image(systemName: "ellipsis")
                        .rotationEffect(Angle.init(degrees: 90))
                        .frame(width: CGFloat(40), height: 40, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                        .offset(x: 8)
                        //test code
                        //.border(Color.black, width: 1)
                }
                /*
                 Bug:
                 */
                .onTapGesture(perform: {
                    //拦截此事件
                })
                
                
            }
            //编辑按钮
            
            
          
            
        }/*HStack*/
//        .onTapGesture {
//            if bookmark.type == "folder" {
//                onBookmarkFolderClickClosure!(bookmark)
//            }
//            else if bookmark.type == "url"{
//                
//            }
//        }/*onTapGesture*/
    }
}

struct BookmarkRow_Previews: PreviewProvider {
    func onDeleteButtonPressed(bookmark: Bookmark){

    }
    static var previews: some View {

        VStack {
            BookmarkRow(bookmark: Bookmark(date_added: Date(), last_visited: Date(), name: "s", url: "ssss"), isDirectory: false, canRowBeSelected: .constant(true), isMoveMode: .constant(true), isSelected: true)
            BookmarkRow(bookmark: Bookmark(date_added: Date(), last_visited: Date(), name: "s", url: "ssss"), isDirectory: false, canRowBeSelected: .constant(false), isMoveMode: .constant(false), isSelected: true)
        }
        
        
    }
}
